package LatentTicTacToe.InformationSets;

import java.io.Serializable;

import LatentTicTacToe.Data.AbstractBoard;
import LatentTicTacToe.Data.Move;

/**
 * Class which holds data for information sets
 * 
 * @author Jiri Cermak
 * 
 */
public class CFRInformationSet extends InformationSet implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * Array holding probability distribution of available moves
	 */
	private float[] strategy;

	/**
	 * Array holding regret of playing move on coordinate i
	 */
	public float[] regret;

	public CFRInformationSet() {
		super();
	}

	public CFRInformationSet(AbstractBoard board, byte currentPlayer, byte winner) {

		super(board.getSuccessors(), currentPlayer, winner);

		this.regret = new float[board.getSuccessors().size()];
		this.strategy = new float[board.getSuccessors().size()];

		for (int i = 0; i < strategy.length; i++) {
			strategy[i] = 1 / (float) strategy.length;
		}
	}

	public String toString() {

		String regretString = "regret: [";
		String valuesString = "values: [";
		String strategyString = "strategy: [";
		String movesString = "";
		String eol = System.getProperty("line.separator");

		movesString = "[" + moves + "]";

		for (int i = 0; i < regret.length; i++) {
			if (regret != null)
				regretString = regretString + regret[i] + ", ";
			if (values != null)
				valuesString = valuesString + values[i] + ", ";
			if (strategy != null)
				strategyString = strategyString + strategy[i] + ", ";
		}

		regretString = regretString + "]";
		valuesString = valuesString + "]";
		strategyString = strategyString + "]";

		return "{" + currentPlayer + eol + movesString + ", " + eol + regretString + ", " + eol + valuesString + ", " + eol + strategyString + ", " + eol + this.getProbability() + "}";
	}

	public float[] getStrategy() {
		return strategy;
	}

	public float getStrategyForMove(Move move) {

		int counter = 0;

		for (Move possibleMove : moves) {
			if (possibleMove.equals(move)) {
				return strategy[counter];
			}
			counter++;
		}

		return -1;
	}

	public void setStrategy(float[] strategy) {
		this.strategy = strategy;
	}

}
